package com.sias.oj.controller;

import com.sias.oj.common.BaseResponse;
import com.sias.oj.common.ErrorCode;
import com.sias.oj.common.ResultUtils;
import com.sias.oj.exception.BusinessException;
import com.sias.oj.model.dto.postthumb.PostThumbAddRequest;
import com.sias.oj.model.entity.User;
import com.sias.oj.service.PostThumbService;
import com.sias.oj.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * 帖子点赞接口
 *
 * @author <a href="https://github.com/sogra">程序员sogra</a>
 */
@RestController
@RequestMapping("/post_thumb")
@Slf4j
public class PostThumbController {

  @Resource
  private PostThumbService postThumbService;

  @Resource
  private UserService userService;

  /**
   * 点赞 / 取消点赞
   *
   * @param postThumbAddRequest
   * @param request
   * @return resultNum 本次点赞变化数
   */
  @PostMapping("/")
  public BaseResponse<Integer> doThumb(@RequestBody PostThumbAddRequest postThumbAddRequest,
                                       HttpServletRequest request) {
    if (postThumbAddRequest == null || postThumbAddRequest.getPostId() <= 0) {
      throw new BusinessException(ErrorCode.PARAMS_ERROR);
    }
    // 登录才能点赞
    final User loginUser = userService.getLoginUser(request);
    long postId = postThumbAddRequest.getPostId();
    int result = postThumbService.doPostThumb(postId, loginUser);
    return ResultUtils.success(result);
  }

}
